
package Taller3;

import Entity.Person;
import Entity.Veterinarian;
import Services.AppointmentService;
import Services.MedicalRecordService;
import Services.NonexistentEntityException;
import Services.OwnerService;
import Services.PersonService;
import Services.PetService;
import Services.VaccineService;
import Services.VeterinarianService;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class Taller3 extends javax.swing.JFrame implements ActionListener{
    
    private JTextField t3, tb;
    
    private JTable table;
    
    private JRadioButton veterinarian, pet, ownerb, SSN, species, all;
    
    private JButton login;
    
    private JButton search, crear, borrar, actualizar, crear1, borrar1, actualizar1;
    
    private JButton cuadro [][] =new JButton[3][3];

    private JButton cuadro1 [][];

    private JButton cuadro11 [][]= new JButton[4][1];;

    private JButton cuadro2[][];

    private JSplitPane splitsearch;

    private JSplitPane panel;

    private JSplitPane panel1;

    private JPanel panel2=new JPanel();
    
    private JPanel panel3=new JPanel();
    
    
    
        Taller3(String titulo,int ancho,int alto)
   {      
       super();
        setTitle(titulo);
        setBackground(new Color(0x00,0x00,0x00) );
        setSize(ancho,alto);
        setLocationRelativeTo(null);
        
        
        
        addWindowListener(new WindowAdapter()
        {      
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
 
            public void windowClosed(WindowEvent e)
            {
                System.exit(0);        
            }
            
        });
        
        panel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

        panel1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);

        JPanel p1 =new JPanel();

        p1.setBackground(Color.white);

        panel.setLeftComponent(panel1);
        
        panel2.add(t3 = new JTextField(10));
        panel2.add(search = new JButton("Buscar"));
        search.addActionListener(this);
        panel2.add(veterinarian= new JRadioButton("Veterinario"));
        panel2.add(pet= new JRadioButton("Mascota"));
        panel2.add(ownerb= new JRadioButton("Propietario"));
        panel2.add(SSN= new JRadioButton("SeguroSocial"));
        panel2.add(all= new JRadioButton("todos"));
       

        panel.setRightComponent(panel2);

        panel1.setBottomComponent(p1);

        panel1.setTopComponent(getCreatePaneltablero1());
        
        setContentPane(panel);
    }
 
    
    public JPanel getCreatePaneltablero1(){

        cuadro1 = new JButton[2][1];

        JPanel paneltablero1 = new JPanel();

        paneltablero1.setLayout(new GridLayout(1,2)); 

        for (int i=0; i< 2 ;i++) {

        if(i==0){

        cuadro1[i][0] = new JButton("Empleado");

        }else{

        cuadro1[i][0] = new JButton("Usuario");

        }

        paneltablero1.add(cuadro1[i][0]);

        cuadro1[i][0].addActionListener(this);	

        }

        return paneltablero1;

     }
    
    public JPanel getCreatePanelLogin(){
        JPanel panellogin = new JPanel();
        
        JTextField t1, t2;
        panellogin.setLayout(new GridLayout(15,1));
        panellogin.add(new JLabel("Usuario:"));
        panellogin.add(t1 = new JTextField(10));
        panellogin.add(new JLabel("Clave:"));
        panellogin.add(t2 = new JTextField(10));
        panellogin.add(login = new JButton("Iniciar Sesión"));
        login.addActionListener(this);

        return panellogin;
        
    }
    public JPanel getCreatePaneltablero(){
           
            JPanel paneltablero = new JPanel();
             paneltablero.setLayout(new GridLayout(6,1));
            
            
            for (int i=0; i< 3 ;i++) {
                            if(i==0){
                                   cuadro[i][0] = new JButton("Crear");
                                   
                                    
                                   
                            }else if(i==1){
                                    cuadro[i][0] = new JButton("Actualizar");
                                    
                            }else if(i==2){
                                    cuadro[i][0] = new JButton("Eliminar");
                                     
                            }
                            
                            paneltablero.add(cuadro[i][0]);
                            cuadro[i][0].addActionListener(this);

                    
            }

            return paneltablero;
    }
    
    
    public Taller3() {
        EntityManagerFactory emf;
        EntityManager em;
        Map<String, String> properties = new HashMap<>();
        
       properties.put("javax.persistance.jdbc.use","root");
       properties.put("javax.persistance.jdbc.password","91082602238");
       
        
        //emf = Persistence.createEntityManagerFactory("HPVAS");
        emf = Persistence.createEntityManagerFactory("HPVAS",properties);
        em = emf.createEntityManager();
        //Veterinarian  veterinarian = new Veterinarian();
        VeterinarianService veterinarianService = new VeterinarianService(emf);
        
        PersonService personService = new PersonService(emf);
        OwnerService ownerService = new OwnerService(emf);
        PetService petService = new PetService(emf);
        MedicalRecordService medicalrecordService = new MedicalRecordService(emf);
        AppointmentService appointmentService = new AppointmentService(emf);
        VaccineService vaccineService = new VaccineService(emf);
        Person person = new Person();
        try{
            
            
          //  veterinarianService.create(veterinarian);
          //  veterinarianService.destroy(veterinarian.getId());
            System.out.println("-----------------------------------PERSON-------------------------------------------");
            System.out.println(personService.findPersonEntities());
            System.out.println("------------------------------VETERINARIAN------------------------------------------");
            System.out.println(veterinarianService.findVeterinarianEntities("ANA"));
            System.out.println("---------------------------------OWNERS---------------------------------------------");
            System.out.println(ownerService.findOwnerEntities());
            System.out.println("---------------------------------PETS-----------------------------------------------");
            System.out.println(petService.findPetEntities());
            System.out.println("------------------------------APPOINTMENT-------------------------------------------");
            System.out.println(appointmentService.findAppointmentEntities());
            System.out.println("--------------------------------VACCINE----------------------------------------------");
            System.out.println(vaccineService.findVaccineEntities());
            System.out.println("-----------------------------MEDICAL RECORD------------------------------------------");
            System.out.println(medicalrecordService.findMedicalRecordEntities());
            
        }catch(Exception e){
        }
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jPanel1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 311, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 192, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(51, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(66, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jPanel1MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
        Taller3 hpvas= new Taller3();
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Taller3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Taller3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Taller3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Taller3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        
         Taller3 test=new Taller3("HPVAS",800,600);
       
        test.setVisible(true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

     @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==cuadro1[0][0]){
            System.out.println("empleado");
            panel.setRightComponent(panel2);
            panel1.setBottomComponent(getCreatePanelLogin());
	}else if(e.getSource()==cuadro1[1][0]){
            panel3.setBackground(Color.white);
            panel1.setBottomComponent(panel3);
            panel2.remove(species);
            panel2.revalidate();
            panel2.repaint();
            panel.setRightComponent(panel2);
            System.out.println("usuario");
        }else if(e.getSource()==login){
            //if(si esta el nombre en BD y I esta la clave en la BD){
            panel2.add(species = new JRadioButton("Especie"));
            panel2.revalidate();
            panel1.setBottomComponent(getCreatePaneltablero());
            //}
        }else if(e.getSource()==cuadro[0][0]){
            //crear
            JPanel panelcrear = new JPanel();
            panelcrear.add(t3 = new JTextField(10));
            panelcrear.add(crear1 = new JButton("Crear"));
            crear1.addActionListener(this);
            panelcrear.add(veterinarian= new JRadioButton("Veterinario"));
            panelcrear.add(pet= new JRadioButton("Mascota"));
            panelcrear.add(ownerb= new JRadioButton("Propietario"));
            panelcrear.add(SSN= new JRadioButton("SeguroSocial"));
            panelcrear.add(all= new JRadioButton("todos"));
       
            
            veterinarian.addActionListener(this);
            pet.addActionListener(this);
            ownerb.addActionListener(this);
            SSN.addActionListener(this);
            all.addActionListener(this);
            
             panel.setRightComponent(panelcrear);
            
        }else if(e.getSource()==cuadro[2][0]){
            //borrar
            
            JPanel panelborrar = new JPanel();
            panelborrar.add(tb = new JTextField(10));
            panelborrar.add(borrar1 = new JButton("Borrar"));
            borrar1.addActionListener(this);
            panelborrar.add(veterinarian= new JRadioButton("Veterinario"));
            panelborrar.add(pet= new JRadioButton("Mascota"));
            panelborrar.add(ownerb= new JRadioButton("Propietario"));
            panelborrar.add(SSN= new JRadioButton("SeguroSocial"));
            panelborrar.add(all= new JRadioButton("todos"));
            
            veterinarian.addActionListener(this);
            pet.addActionListener(this);
            ownerb.addActionListener(this);
            SSN.addActionListener(this);
            
            
            panel.setRightComponent(panelborrar);
        }else if(e.getSource()==cuadro[1][0]){
            //actualizar
            
            JPanel panelactualizar = new JPanel();
            panelactualizar.add(t3 = new JTextField(10));
            panelactualizar.add(actualizar1 = new JButton("Actualizar"));
            actualizar1.addActionListener(this);
            panelactualizar.add(veterinarian= new JRadioButton("Veterinario"));
            panelactualizar.add(pet= new JRadioButton("Mascota"));
            panelactualizar.add(ownerb= new JRadioButton("Propietario"));
         
         
            
            veterinarian.addActionListener(this);
            pet.addActionListener(this);
            ownerb.addActionListener(this);
            SSN.addActionListener(this);
            
            panel.setRightComponent(panelactualizar);
        }else if(e.getSource()==search){
            //
            
            if(e.getSource()==veterinarian){
                             
                    Services.VeterinarianService vetr = new VeterinarianService(null);
                    vetr.findVeterinarianEntities(t3.getText());
              
            }else if(e.getSource()==pet){
               
                    Services.PetService petr = new PetService(null);
                    petr.findPet(Long.parseLong(tb.getText()));
                             
            
            }else if(e.getSource()==ownerb){
                                  
                    Services.OwnerService owner = new OwnerService(null);
                    owner.findOwnerEntities();
                
               
            }
            
            
        }else if(e.getSource()==crear1){
                System.out.println("crear!");
        }else if(e.getSource()==borrar1){
            if(e.getSource()==veterinarian){
                try {                
                    Services.VeterinarianService vetr = new VeterinarianService(null);
                    vetr.destroy(Long.parseLong(tb.getText()));
                } catch (NonexistentEntityException ex) {
                    Logger.getLogger(Taller3.class.getName()).log(Level.SEVERE, null, ex);
                }
            }else if(e.getSource()==pet){
                try {                
                    Services.PetService petr = new PetService(null);
                    petr.destroy(Long.parseLong(tb.getText()));
                } catch (NonexistentEntityException ex) {
                    Logger.getLogger(Taller3.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            
            }else if(e.getSource()==ownerb){
                try {                              
                    Services.OwnerService owner = new OwnerService(null);
                    owner.destroy(Long.parseLong(tb.getText()));
                } catch (NonexistentEntityException ex) {
                    Logger.getLogger(Taller3.class.getName()).log(Level.SEVERE, null, ex);
                }
               
            }
        }else if(e.getSource()==actualizar){
            //    
        }else{
		System.out.println("The Button Works!");
        }
    }
    
}
